package com.mrlee.controller;

import com.github.pagehelper.PageInfo;
import com.mrlee.bean.Cost;
import com.mrlee.service.CostService;
import com.mrlee.utils.TimeUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by dllo on 18/3/13.
 */
@Controller
public class CostController {

    @Resource
    private CostService costService;

    /**
     * 跳转到fee_list
     * @return
     */
    @RequestMapping(value = "fee")
    public String fee(){
        return "fee/fee_list";
    }

    /**
     * 跳转到fee_add
     * @return
     */
    @RequestMapping(value = "add")
    public String add(){
        return "fee/fee_add";
    }

    /**
     * 跳转到fee_modi
     * @return
     */
    @RequestMapping(value = "modi")
    public String modi(){
        return "fee/fee_modi";
    }

    /**
     * 跳转到fee_detail
     * @return
     */
    @RequestMapping(value = "detail")
    public String detail(){
        return "fee/fee_detail";
    }

    /**
     * 查询所有cost
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "allcost")
    public List<Cost> allCost(){
        return costService.selectAllCost();
    }

    /**
     * fee分页
     * @param pageNo
     * @param pageSize
     * @return
     */
    @RequestMapping(value = "costpage")
    @ResponseBody
    public PageInfo<Cost> adminByPage(@RequestParam(value = "pagenum",required = false)Integer pageNo,
                                      @RequestParam(value = "pagesize",required = false)Integer pageSize){
        return costService.queryCostByPage(pageNo,3);
    }

    /**
     * 查询所有开状态的资费
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/allopencost")
    public List<Cost> allOpenCost(){
        return costService.selectAllOpenCost();
    }

    /**
     * 按基费升序排列
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/allcostascbybc")
    public List<Cost> allcostascbybc(){
        return costService.selectAllAscBybc();
    }

    /**
     * 按时长升序排列
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/allcostascbybd")
    public List<Cost> allcostascbybd(){
        return costService.selectAllAscBybd();
    }

    /**
     * 按基费降序排列
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/allcostdescbybc")
    public List<Cost> allcostdescbybc(){
//        System.out.println(costService.selectAllDescBybc());
        return costService.selectAllDescBybcost();
    }

    /**
     * 按时长降序排列
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/allcostdescbybd")
    public List<Cost> allcostdescbybd(){
        return costService.selectAllDescBybd();
    }

    /**
     * 删除的方法
     * @param id
     * @return
     */
    @RequestMapping(value = "/delete")
    public String delete(@Param("id")Integer id){
//        System.out.println(id);
        costService.deleteCostById(id);
        return "fee/fee_list";
    }

    /**
     * 点击开通的方法
     * @param id
     * @return
     */
    @RequestMapping(value = "/start")
    public String start(@Param("id")Integer id){
        costService.updateStById(id);
        return "fee/fee_list";
    }

    /**
     * 按id查cost
     * @param costid
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/costbyid")
    public Cost byId(@RequestParam("costid")Integer costid){
        System.out.println(costid);
        return costService.selectById(costid);
    }

    /**
     * 按id修改1
     * @param costid
     * @param costname
     * @param costtype
     * @param baseduration
     * @param basecost
     * @param unitcost
     * @param descr
     * @return
     */
    @RequestMapping(value = "/upbyid")
    public String upbyid(@Param("costid")Integer costid,
                      @Param("costname")String costname,
                      @RequestParam("radFeeType")String costtype,
                      @Param("baseduration")Integer baseduration,
                      @Param("basecost")Integer basecost,
                      @Param("unitcost")Integer unitcost,
                      @Param("descr")String descr){
        Cost cost = new Cost();
        cost.setCostId(costid);
        cost.setName(costname);
        cost.setCostType(costtype);
        cost.setBaseDuration(baseduration);
        cost.setBaseCost(basecost);
        cost.setUnitCost(unitcost);
        cost.setDescr(descr);
        System.out.println(cost);
        costService.updateByPrimaryKeySelective(cost);
        return "fee/fee_list";
    }

    /**
     * 添加的方法
     * @param costname
     * @param costtype
     * @param baseduration
     * @param basecost
     * @param unitcost
     * @param descr
     * @return
     */
    @RequestMapping(value = "/insert")
    public String insert(@Param("costname")String costname,
                         @RequestParam("radFeeType")String costtype,
                         @Param("baseduration")Integer baseduration,
                         @Param("basecost")Integer basecost,
                         @Param("unitcost")Integer unitcost,
                         @Param("descr")String descr){
        Cost cost = new Cost();
        cost.setName(costname);
        cost.setCostType(costtype);
        cost.setBaseDuration(baseduration);
        cost.setBaseCost(basecost);
        cost.setUnitCost(unitcost);
        cost.setDescr(descr);
        cost.setStatus("停");
        cost.setCreatime(TimeUtils.time());
        costService.insert(cost);
        System.out.println(cost);
        return "fee/fee_list";
    }
}
